import {
  queryOrderDetail,
  buyOutUserOrderDetail,
  queryDeliverOps,
  sendDeliver,
  addRemark,
  closeOrder,
  settleOrder,
  generateReportByOrderId,
  submitRemark,
  getDetailinformation,
} from '@/services/api';
import { message } from 'antd';

export default {
  namespace: 'detail',

  state: {
    detailInfo: {},
    modalVisible: false,
    deliverOptions: [],
    modalStyle: 'deliver', // remark
    radioValue: 'good',
    checkValue: false,
    damageVal: 0, // 损坏
    reportDetal: {},
    newDetailInfo: {},
  },

  effects: {
    *queryOrderDetail({ payload }, { call, put }) {
      const response = yield call(queryOrderDetail, payload.id);
      const detailInfo = response.data;
      yield put({
        type: 'save',
        payload: {
          detailInfo,
        },
      });
    },
    *generateReportByOrderId({ payload, callback }, { call, put }) {
      const response = yield call(generateReportByOrderId, payload);
      if (response.data) {
        yield put({
          type: 'save',
          payload: {
            reportDetal: response.data,
          },
        });
      }
      callback && callback(response);
    },
    *buyOutUserOrderDetail({ payload }, { call, put }) {
      const response = yield call(buyOutUserOrderDetail, payload.id);
      const detailInfo = response.data;
      if (detailInfo.userViolationRecords && detailInfo.userViolationRecords[0]) {
        let radioValue = 'good';
        radioValue = detailInfo.userOrderCashes.damagePrice ? 'damage' : radioValue;
        radioValue = detailInfo.userOrderCashes.lostPrice ? 'lose' : radioValue;
        yield put({
          type: 'save',
          payload: {
            checkValue: true,
            radioValue,
            damageVal:
              detailInfo.userOrderCashes.damagePrice || detailInfo.userOrderCashes.lostPrice,
          },
        });
      }
      yield put({
        type: 'save',
        payload: {
          detailInfo,
        },
      });
    },
    // eslint-disable-next-line no-unused-vars
    *queryDeliverOps({ payload }, { call, put }) {
      const response = yield call(queryDeliverOps);
      yield put({
        type: 'save',
        payload: {
          deliverOptions: response.data,
        },
      });
    },

    *addRemark({ payload }, { call, put }) {
      const res = yield call(addRemark, payload);
      if (res.code) {
        message.success('备注成功');
      }
      yield put({
        type: 'save',
        payload: {
          modalVisible: false,
        },
      });
      yield put({
        type: 'queryOrderDetail',
        payload: {
          id: payload.orderId,
        },
      });
    },
    *pushRemark({ payload, callback }, { call, put }) {
      const res = yield call(submitRemark, payload);
      if (res.code) {
        yield put({
          type: 'save',
          payload: {
            modalVisible: false,
          },
        });
        message.success('备注成功');
        callback();
      }
    },
    *sendDeliver({ payload }, { call, put }) {
      yield call(sendDeliver, payload);
      yield put({
        type: 'save',
        payload: {
          modalVisible: false,
        },
      });
      yield put({
        type: 'queryOrderDetail',
        payload: {
          id: payload.orderId,
        },
      });
    },
    *closeOrder({ payload }, { call, put }) {
      yield call(closeOrder, payload);
      message.success('关闭成功');
      yield put({
        type: 'queryOrderDetail',
        payload: {
          id: payload.orderId,
        },
      });
    },
    *settleOrder({ payload, callback }, { call, put }) {
      let res = yield call(settleOrder, payload);
      callback(res);
      message.success('操作成功');
      yield put({
        type: 'queryOrderDetail',
        payload: {
          id: payload.orderId,
        },
      });
    },
    //获取详情页面信息
    *getDetailinformation({ payload, callback }, { call, put }) {
      const response = yield call(getDetailinformation, payload.id);
      yield put({
        type: 'detailStorage',
        payload: response.data,
      });
      callback(response);
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    detailStorage(state, { payload }) {
      return {
        ...state,
        newDetailInfo: payload,
      };
    },
  },
};

/**
 * WAITING_PAYMENT  为1
WAITING_BUSINESS_DELIVERY 为2
WAITING_USER_RECEIVE_CONFIRM 为3
WAITING_GIVE_BACK 为4
WAITING_SETTLEMENT,WAITING_SETTLEMENT_PAYMENT 为5
其他的为6

用户信息
orderAddress
地址 provinceStr cityStr areaStr street
收货人 realname
电话 telephone
买家留言
userOrders里面的remark 有值显示字段   userOrders.nums   userOrders 下的nums

订单支付信息
product
skuTitle 规格
images src 产品图片
商品名称 productName

userOrderCashes
日租金rent
数量  userOrders.nums
租期 userOrders.rentDuration
总押金 totalDeposit
押金减免 depositReduction
冻结押金 deposit
总租金 totalRent
实付押金去掉 这个字段和冻结押金一样的值
运费 freightPrice
平台优惠 platformCouponReduction
店铺优惠 couponReduction
订单状态  userOrders.status

订单基本信息
userOrders里面
昵称改成真实姓名 realName
订单状态 status
起租 rentStart  和unrentTime
发货方式 logisticForm 0快速发货
联系电话telephone
下单时间 createTime
 */
